<?php

/**
 * Security actions.
 *
 * @package    ProdAssistant
 * @subpackage Security
 * @author     Paweł Świtalla
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class SecurityActions extends sfActions
{
  private function setAuthenticated($authenticated, $user)
  {
    $this->getUser()->setAuthenticated($authenticated);
    $this->getUser()->setAttribute('userObject', $user);
  }
  
  public function executeLogin(sfWebRequest $request)
  {
    $this->form = new sfForm();
    $this->form->setWidgets(array(
        'login' => new sfWidgetFormInputText(),
        'password' => new sfWidgetFormInputPassword(array('label' => 'Hasło'))
    ));
    $this->form->setValidators(array(
        'login' => new sfValidatorString(array(), array('required' => 'Pole wymagane.')),
        'password' => new sfValidatorString(array(), array('required' => 'Pole wymagane.'))
    ));
    $this->form->getWidgetSchema()->setNameFormat('user[%s]');
    $this->setLayout('login');
    if($request->isMethod('post'))
    {
      $this->form->bind($request->getParameter('user'));
      if($this->form->isValid())
      {
        $user = Doctrine_Core::getTable('User')->findBy('login', $this->form->getValue('login'))->getFirst();
        if($user instanceof User){
          if($user->getPassword() === md5($user->getSalt().$this->form->getValue('password'))){
            $this->setAuthenticated(true, $user);
            $time = new DateTime();
            $user->setLastLogon($time->format('Y-m-d H:i:s'));
            $user->save();
            if($user->Role->getIsAdmin()){
              $this->getUser()->addCredentials('admin');
              $this->getUser()->addCredentials('user');
            }
            else $this->getUser()->addCredentials('user');
            $this->redirect(PACore::getInstance()->generateAppUrl('Store', 'categoryHomepage'));
          }
        }
        else
        {
          $this->getUser()->setFlash('info', 'Podałeś zły login lub hasło');
          $this->redirect($request->getReferer());
        }
      }
    }
  }
  
  public function executeSecure(sfWebRequest $request)
  {
    if(!$request->isXmlHttpRequest()){
      $this->form = new sfForm();
      $this->form->setWidgets(array(
          'login' => new sfWidgetFormInputText(),
          'password' => new sfWidgetFormInputPassword(array('label' => 'Hasło'))
      ));
      $this->form->setValidators(array(
          'login' => new sfValidatorString(array(), array('required' => 'Pole wymagane.')),
          'password' => new sfValidatorString(array(), array('required' => 'Pole wymagane.'))
      ));
      $this->form->getWidgetSchema()->setNameFormat('user[%s]');
      $this->setLayout('login');
      if($request->isMethod('post'))
      {
        $this->form->bind($request->getParameter('user'));
        if($this->form->isValid())
        {
          $user = Doctrine_Core::getTable('User')->findBy('login', $this->form->getValue('login'))->getFirst();
          if($user instanceof User){
            if($user->getPassword() === md5($user->getSalt().$this->form->getValue('password'))){
              $this->setAuthenticated(true, $user);
              if($user->Role->getIsAdmin()) $this->getUser()->addCredentials('admin');
              $this->redirect($request->getReferer());
            }
          }
        }
      }
    }
    else{
      $core = PACore::getInstance();
      $this->getResponse()->setContent('location@'.$core->generateAppUrl('Core', 'secure')."#");
      return sfView::NONE;
    }
  }
  
  public function executeLogout(sfWebRequest $request)
  {
    $this->getUser()->clearCredentials();
    $this->getUser()->setAuthenticated(false);
    $this->getUser()->setAttribute('userObject', false);
    $this->redirect(PACore::getInstance()->generateAppUrl('Core', 'homepage'));
  }
  
  public function executeIndex(sfWebRequest $request)
  {
    $this->redirect(PACore::getInstance()->generateAppUrl('Core', 'homepage'));
  }
}
